home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / SNNSV32.ZIP / SNNSv3.2 / kernel / sources / calc_err.c < prev    next >
C/C++ Source or Header  |  1994-04-25  |  2KB  |  108 lines

  1. /*****************************************************************************
  2.   FILE           : calc_err.c
  3.   SHORTNAME      : calc_err
  4.   SNNS VERSION   : 3.2
  5.  
  6.   PURPOSE        : SNNS-Kernel Tool: Calculates the error of a table-lookup function
  7.   NOTES          :
  8.  
  9.   AUTHOR         : Niels Mache
  10.   DATE           : 06.12.91
  11.  
  12.   CHANGED BY     : Sven Doering
  13.   IDENTIFICATION : @(#)calc_err.c    1.9 3/15/94
  14.   SCCS VERSION   : 1.9
  15.   LAST CHANGE    : 3/15/94
  16.  
  17.              Copyright (c) 1990-1994  SNNS Group, IPVR, Univ. Stuttgart, FRG
  18.  
  19. ******************************************************************************/
  20. #include <stdio.h>
  21. #include <math.h>
  22.  
  23.  
  24. #define MINUS_INFINITE_BORDER  -100.0
  25. #define PLUS_INFINITE_BORDER    100.0
  26. #define CALC_STEPS 200000
  27.  
  28.  
  29.  
  30. /* ***************************************************************** */
  31.  
  32. /*  Sigmoid Function
  33. */
  34. double    f( x )
  35. double    x;
  36. {
  37.   return( 1.0 / (1.0 + exp( -x )));
  38. }
  39.  
  40. /*  Sigmoid Function
  41.     using table lookup and linear approximation method
  42. */
  43. double     LogisticTbl( x )
  44. double    x;
  45. {
  46. #include "sigmoid.tbl"
  47.  
  48.   register int    index;
  49.  
  50.  
  51.   index = (int) (x * SCALE_FACTOR) + INDEX_OFFSET;
  52.  
  53.   if (index < 0)
  54.     {  /*  x is less then MIN_APPROX_X:
  55.        approx. func value to MINUS_INFINITE_FUNC_VALUE  */
  56.     if (index <= MIN_INDEX)
  57.       { /*  printf( "x below -infinite : %g\n", x );  */
  58.     return( MINUS_INFINITE_FUNC_VALUE );
  59.       }
  60.     /*    printf( "x below MIN_APPROX_X : %g\n", x );  */
  61.     return( m[0] * x + b[0] );
  62.     }
  63.  
  64.   if (index > NO_OF_APPROX)
  65.     {  /*  x is greater then MAX_APPROX_X:
  66.        approx. func value to PLUS_INFINITE_FUNC_VALUE  */
  67.     if (index >= MAX_INDEX)
  68.       { /*  printf( "x above +infinite : %g\n", x );  */
  69.     return( PLUS_INFINITE_FUNC_VALUE );
  70.       }
  71.     /*    printf( "x above MAX_APPROX_X : %g\n", x );  */
  72.     return( m[ NO_OF_APPROX ] * x + b[ NO_OF_APPROX ] );
  73.     }
  74.  
  75.   return( m[ index ] * x + b[ index ] );
  76. }
  77.  
  78.  
  79. /* ***************************************************************** */
  80.  
  81.  
  82.  
  83.  
  84. int  main()
  85. {
  86.   double  x, step, y1, y2, devit;
  87.   int      i, nl_cycle;
  88.  
  89.  
  90.   devit = 0.0;
  91.   step = (PLUS_INFINITE_BORDER - MINUS_INFINITE_BORDER) / (double) CALC_STEPS;
  92.  
  93.   x = MINUS_INFINITE_BORDER;
  94.   y1 = fabs( f(x) - LogisticTbl( x ) );
  95.   x += step;
  96.  
  97.   for (i = 0; i < CALC_STEPS - 1; i++)
  98.     {
  99.     y2 = fabs( f(x) - LogisticTbl( x ) );
  100.     devit += 0.5 * (y1 + y2) * step;
  101.     y1 = y2;
  102.     x += step;
  103.     }
  104.  
  105.   fprintf( stdout, "Error in [%+g,%+g]: %.10g\n",
  106.        MINUS_INFINITE_BORDER, PLUS_INFINITE_BORDER, devit );
  107. }
  108.